layui.use(['layer', 'form', 'admin', 'ax', 'upload', 'jquery', 'table'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var table = layui.table;
    //初始化所有的产品类型列表
    $.ajax({
        url: Feng.ctxPath + '/productType/list',
        dataType: 'json',
        type: 'get',
        data: {},
        success: function (data) {
            $('#productTypeId').append('<option value="">请选择产品类型</option>');
            $.each(data.data, function (index, value) {
                $('#productTypeId').append(new Option(value.name, value.id));// 下拉菜单里添加元素
            });
            layui.form.render('select');
        }
    });

    $("#productTypeId").change(function () {
        $.ajax({
            url: Feng.ctxPath + '/productAttr/detail/' + $(this).val(),
            dataType: 'json',
            type: 'get',
            data: {},
            success: function (result) {
                var attrContext = result.data.attrContext;
                console.log("属性值:" + attrContext);
                var tableData = [];
                if (attrContext != null) {
                    var attrJson = JSON.parse(attrContext);

                    var i = 0;
                    for (var key in attrJson) {
                        i++;
                        var id = result.data.id + '_' + i;
                        var obj = {'id': id};
                        obj['productAttrName'] = key;
                        obj['productAttr'] = '';
                        obj['productAttrValue'] = attrJson[key];
                        tableData.push(obj);
                    }
                    table.render({
                        elem: '#productAttrTable',
                        id: "productAttrTable",
                        cellMinWidth: 95,
                        // toolbar: "#toolbar",
                        defaultToolbar: [],
                        height: "full-160",
                        text: {none: "暂无数据"},
                        page: false,
                        skin: 'line ',
                        cols: [
                            [
                                {
                                    field: 'id',
                                    title: 'ID',
                                    hide: true
                                },
                                {
                                    type: "checkbox",
                                    fixed: 'left',
                                    width: "10%"
                                },
                                {
                                    field: 'productAttrName',
                                    title: '属性名称',
                                    width: "30%",
                                    align: 'center'
                                },
                                {
                                    field: 'productAttr',
                                    title: '值',
                                    width: "30%",
                                    align: 'center',
                                    edit: 'text'
                                },
                                {
                                    field: 'productAttrValue',
                                    title: '单位',
                                    width: "30%",
                                    align: 'center'
                                }
                            ]
                        ],
                        data: tableData
                    });

                }
            }
        });
    })


    // 表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        var attrData = table.cache["productAttrTable"];
        var json = {};

        for (var i = 0; i < attrData.length; i++) {
            var attr = attrData[i];
            if (attr.productAttr) {
                console.log(attr.id)
                if(!data.field.attrId){
                    data.field.attrId = attr.id.split("_")[0]
                }
                json[attr.productAttrName] = attr.productAttr + "_" + attr.productAttrValue;
                data.field.exAttr = JSON.stringify(json);
            }

        }


        var ajax = new $ax(Feng.ctxPath + "/product/add", function (data) {
            Feng.success("添加成功！");

            //传给上个页面，刷新table用
            admin.putTempData('formOk', true);

            //关掉对话框
            admin.closeThisDialog();

        }, function (data) {
            Feng.error("添加失败！" + data.responseJSON.message)
        });
        ajax.set(data.field);
        ajax.start();

        //添加 return false 可成功跳转页面
        return false;
    });

});
